package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.annotation.SystemServiceLog;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootChip;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.form.impl.FootChipSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootChipMapper;
import org.footballmanager.pandafootball.system.service.FootChipService;
import org.footballmanager.pandafootball.system.service.FootPersonService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.FootZnxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootChipServiceImpl extends ServiceImpl<FootChip> implements FootChipService {
	/*白色	75以下	 5个碎片随机合成一个
	      绿色	75-79	10个碎片随机合成一个
	      蓝色	80-83	15个碎片随机合成一个
	      紫色	84-89	20个碎片随机合成一个
	      橙色	90-100	30个碎片随机合成一个*/
	private static final long WHITE = 5;
	private static final long GREEN = 10;
	private static final long BLUE = 15;
	private static final long PURPLE = 20;
	private static final long ORANGE = 30;

	@Autowired
	private FootPersonService footPersonService;
    @Autowired
    private FootChipMapper footChipMapper;
    @Autowired
    private FootZnxService footZnxService;
    @Autowired
    private FootUserService footUserService;

    @Override
    @Transactional
    @SystemServiceLog(description = "更新碎片记录")
    public int updateNotNull(FootChip entity, HttpServletRequest request){
        if(null != entity.getContext()){
            footZnxService.sendZnx(footUserService.getAdminUserid(), entity.getUserId(),
                    entity.getContext(), null, null, request);
        }
        return super.updateNotNull(entity, request);
    }

	@Override
	public String randomBluePerson(Long userId, HttpServletRequest request) {
		FootChip chip = this.findChipByUserId(userId);
		if(chip.getHaveBlue() >= BLUE){
			chip.setHaveBlue(chip.getHaveBlue() - BLUE);
			this.updateNotNull(chip, request);
			return footPersonService.findPersonByScore("80", "83", userId, request);
		}
		return null;
	}

	@Override
	public String randomGreenPerson(Long userId, HttpServletRequest request) {
		FootChip chip = this.findChipByUserId(userId);
		if(chip.getHaveGreen() >= GREEN){
			chip.setHaveGreen(chip.getHaveGreen() - GREEN);
			this.updateNotNull(chip, request);
			return footPersonService.findPersonByScore("75", "79", userId, request);
		}
		return null;

	}

	@Override
	public String randomOrangePerson(Long userId, HttpServletRequest request) {
		FootChip chip = this.findChipByUserId(userId);
		if(chip.getHaveOrange() >= ORANGE){
			chip.setHaveOrange(chip.getHaveOrange() - ORANGE);
			this.updateNotNull(chip, request);
			return footPersonService.findPersonByScore("90", null, userId, request);
		}
		return null;
	}

	@Override
	public String randomPurplePerson(Long userId, HttpServletRequest request) {
		FootChip chip = this.findChipByUserId(userId);
		if(chip.getHavePurple() >= PURPLE){
			chip.setHavePurple(chip.getHavePurple() - PURPLE);
			this.updateNotNull(chip, request);
			return footPersonService.findPersonByScore("84", "89", userId, request);
		}
		return null;
	}

	@Override
	public String randomWhiltePerson(Long userId, HttpServletRequest request) {
		FootChip chip = this.findChipByUserId(userId);
		if(chip.getHaveWhile() >= WHITE){
			chip.setHaveWhile(chip.getHaveWhile() - WHITE);
			this.updateNotNull(chip, request);
			return footPersonService.findPersonByScore("0", "74", userId, request);
		}
		return null;
	}

	@Override
	public FootChip findChipByUserId(Long userId) {
		Example example = new Example(FootChip.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(Constants.USERID, userId);
		return this.findByExample(example);
	}

	@Override
	public void randomGetChip(Long userId, int type, int num, HttpServletRequest request) {
		FootChip chip = this.findChipByUserId(userId);
		if(null != chip){
			switch (type) {
			case 1:
				chip.setHaveWhile(chip.getHaveWhile() + num);
				break;
			case 2:
				chip.setHaveGreen(chip.getHaveGreen() + num);
				break;
			case 3:
				chip.setHaveBlue(chip.getHaveBlue() + num);
				break;
			case 4:
				chip.setHavePurple(chip.getHavePurple() + num);
				break;
			case 5:
				chip.setHaveOrange(chip.getHaveOrange() + num);
				break;
			default:
				break;
			}
		}
		this.updateNotNull(chip, request);
	}

	@Override
	public void saveNewChip(FootUser user, HttpServletRequest request) {
		FootChip chip = new FootChip();
		chip.setUserId(user.getId());
		chip.setHaveWhile(0L);
		chip.setHaveGreen(0L);
		chip.setHaveBlue(0L);
		chip.setHavePurple(0L);
		chip.setHaveOrange(0L);
		this.save(chip, request);
	}

    /**
     * 翻页方法
     *
     * @param footChipSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootChip> findall(FootChipSearchForm footChipSearchForm) {
        return footChipMapper.findall(footChipSearchForm);
    }

    /**
     * 根据主键获取记录
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootChip get(Serializable id) {
        return footChipMapper.findById(id);
    }

    /**
     * 查询
     *
     * @param entityname         实体类名
     * @param map                参数
     * @param footChipSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootChipSearchForm footChipSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footChipSearchForm);
        Page<FootChip> page = PageHelper.startPage(footChipSearchForm.getPageIndex(), footChipSearchForm.getPageSize());
        List<FootChip> list = this.findall(footChipSearchForm);
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
        map.addAttribute(Constants.PI, page.toPageInfo());
    }
}
